﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class SinglePageEdit : BasePage
    {
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitTree();
            }
        }

        /// <summary>
        /// 加载树
        /// </summary>
        private void InitTree()
        {
            this.CatalogueTree1.CatalogueLogicProvider = catalogueLogic;
            this.CatalogueTree1.CanClickNodeType = CatalogueTypeEnum.SinglePage;

        }

        protected void CatalogueTree1_SelectedNodeChanged(object sender, DelegateAndArges.CatalogueTreeSelectedNodeChangedEventArgs e)
        {
            Catalogue selectedCatalogue = catalogueLogic.GetList(p => p.Id == e.SelectedCatalogueId).FirstOrDefault();
            if (selectedCatalogue != null)
            {
                this.txtId.Value = selectedCatalogue.Id.ToString();
                this.txtCatalogueTitle.Text = selectedCatalogue.Title;
                this.txtCatalogueContent.Text = selectedCatalogue.Content;
            }
        }

        protected override void InitLogic()
        {
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }

        protected void SavePushButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (ckbIsRedirect.Checked)
                {
                    if (string.IsNullOrEmpty(txtReDirectUrl.Text))
                    {
                        msgLabel.Text = "勾选转向复选框，必须填写转到地址。";
                    }
                }

                long id = long.Parse(txtId.Value);
                Catalogue selectedCatalogue = catalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
                if (selectedCatalogue != null)
                {
                    selectedCatalogue.Title = txtCatalogueTitle.Text;
                    selectedCatalogue.ReDirect = ckbIsRedirect.Checked;
                    selectedCatalogue.ReDirectUrl = txtReDirectUrl.Text;
                    selectedCatalogue.Content = txtCatalogueContent.Text;
                }
                catalogueLogic.Save(selectedCatalogue);

                // 更新缓存
                Catalogue cached = catalogueLogic.GetByIdFromCache(id);
                BasicCache.AllCatalogueList.Remove(cached);
                BasicCache.AllCatalogueList.Add(selectedCatalogue);
                AddScript("alert('保存单页内容成功！');");
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('保存单页内容失败！');");
            }
        }
    }
}